<?php
include_once 'field.class.php';

class field_title extends field
{
    public $error;
    protected $fieldname = '单行文本';

    /**
     * 生成设置项
     * @author menghao
     * @param <string> $data
     * @return <string>
     */
    function settings($data='')
    {
        $data = unserialize($data);
        $data['size'] = isset($data['size']) && is_numeric ($data['size']) ? $data['size'] : 50;
        $data['minlength'] = isset($data['minlength']) && is_numeric ($data['minlength']) ? $data['minlength'] : 3;
        $data['maxlength'] = isset($data['maxlength']) && is_numeric ($data['maxlength']) ? $data['maxlength'] : 200;
        
        $setting = '';
        $setting .= '<label>文本框长度：'.form::text('settings[size]', 'setting_size', $data['size'], 7).'</label> ';
        $setting .= '<label>文本最小长度(字节)：'.form::text('settings[minlength]', 'setting_minlength', $data['minlength'], 7).'</label> ';
        $setting .= '<label>文本最大长度(字节)：'.form::text('settings[maxlength]', 'setting_maxlength', $data['maxlength'], 7).'</label> ';
        
        return $setting;
    }


    /**
     * 检查设置项数据
     * @author menghao
     * @param <array> $data 
     */
    function check_settings($data)
    {
        if(!is_numeric($data['size']))
        {
            $this->error = "文本框长度必须是数字";
            return false;
        }
        if(!is_numeric($data['minlength']))
        {
            $this->error = "内容最小长度必须是数字";
            return false;
        }
        if(!is_numeric($data['maxlength']))
        {
            $this->error = "内容最大长度必须是数字";
            return false;
        }
        return true;
    }

    function check_data($data)
    {
        if(strlen($data) < $this->field_data['settings']['minlength'])
        {
            $this->error = "{$this->field_data['name']}最小长度不能少于 {$this->field_data['settings']['minlength']}字节！";
            $this->response->text_alert('error', $this->field_data['field'], $this->error);
            return false;
        }
        elseif(strlen($data) > $this->field_data['settings']['maxlength'])
        {
            $this->error = "{$this->field_data['name']}最大长度不能超过 {$this->field_data['settings']['maxlength']}字节！";
            $this->response->text_alert('error', $this->field_data['field'], $this->error);
            return false;
        }
        return parent::check_data($data);
    }

    function create_form($value='')
    {
        $field = $this->field_data['field'];
        $size = $this->field_data['settings']['size'];
        $form = form::text("data[$field]", $field, $value, $size);
        return $form;
    }
}
?>